<?php

/**
*	Obtiene los restaurantes del hotel, para mostrar en la lista
*   api_key: Api proporcionada por somxarxa para el hotel
* 	id_hotel: Identificador del hotel
* 	idioma: Idioma para obtener la info [0,1,2]
*/
function getRestaurantes() {
	global $app;
	$app->getLog()->info("getRestaurantes");

	$api_key = getAPIKey();
	$id_hotel = getIdHotel();
	
	if(!isValidAPIKey($id_hotel, $api_key)){
		$app->getLog()->error("API_KEY ERROR: " . $id_hotel . " API_KEY: " . $api_key);
		echo '{"error":{"text":'. ERROR_API_KEY .'}}'; 
		return -1;
	}
	
	$idioma = getIdioma();

	$sql_traduccion = "(SELECT * FROM " . TABLA_RESTAURANTE . " res1, " . TABLA_RESTAURANTE_LOCALE . " res1loc WHERE res1.id = res1loc.id_restaurante and res1.id_hotel = :id_hotel and res1loc.id_idioma = :idioma)";
	$sql_sin_traduccion = "(SELECT * FROM " . TABLA_RESTAURANTE . " res2, " . TABLA_RESTAURANTE_LOCALE . " res2loc WHERE res2.id = res2loc.id_restaurante and res2.id_hotel = :id_hotel and NOT EXISTS (SELECT * FROM " . TABLA_RESTAURANTE . " res_sub, " . TABLA_RESTAURANTE_LOCALE . " resloc_sub WHERE res2.id = res2loc.id_restaurante and res2.id = res_sub.id and res2.id = resloc_sub.id_restaurante and res_sub.id_hotel = :id_hotel and resloc_sub.id_idioma = :idioma) GROUP BY(res2.id))";

	$sql = $sql_traduccion . " UNION " . $sql_sin_traduccion;
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql);
		$stmt->bindParam("idioma", $idioma);
		$stmt->bindParam("id_hotel", $id_hotel);
		$stmt->execute();
		$restaurantes = $stmt->fetchAll(PDO::FETCH_OBJ);
		$db = null;
		echo $_GET['callback']."(".json_encode($restaurantes).");";
		
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

//Obtenemos un Restaurante en concreto con su id
function getRestaurante($id) {
	global $app;
	$app->getLog()->info("getRestaurante: " . $id);

	$api_key = getAPIKey();
	$id_hotel = getIdHotel();
	
	if(!isValidAPIKey($id_hotel, $api_key)){
		$app->getLog()->error("API_KEY ERROR: " . $id_hotel . " API_KEY: " . $api_key);
		echo '{"error":{"text":'. ERROR_API_KEY .'}}'; 
		return -1;
	}
	
	$idioma = getIdioma();
	
	$sql = "SELECT * FROM " . TABLA_RESTAURANTE . " rest, " . TABLA_RESTAURANTE_LOCALE . " rest_loc WHERE rest.id = rest_loc.id_restaurante and rest.id_hotel = :id_hotel and rest_loc.id_idioma =:idioma and rest.id=:id";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql);  
		$stmt->bindParam("id", $id);
		$stmt->bindParam("idioma", $idioma);
		$stmt->bindParam("id_hotel", $id_hotel);
		$stmt->execute();
		$restaurante = $stmt->fetchObject();  
		$db = null;
		echo $_GET['callback']."(".json_encode($restaurante).");";
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

//Obtenemos las galeria de un Restaurante
function getRestauranteGaleria($id) {
	global $app;
	$app->getLog()->info("getRestauranteGaleria: " . $id);

	$api_key = getAPIKey();
	$id_hotel = getIdHotel();
	
	if(!isValidAPIKey($id_hotel, $api_key)){
		$app->getLog()->error("API_KEY ERROR: " . $id_hotel . " API_KEY: " . $api_key);
		echo '{"error":{"text":'. ERROR_API_KEY .'}}'; 
		return -1;
	}
	
	$idioma = getIdioma();
	
	$sql = "SELECT * FROM " . TABLA_RESTAURANTE_GALERIA . " rest, " . TABLA_RESTAURANTE_GALERIA_LOCALE . " rest_loc WHERE rest.id = rest_loc.id_restaurante_galeria and rest_loc.id_idioma =:idioma and rest.id_restaurante=:id";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql);  
		$stmt->bindParam("id", $id);
		$stmt->bindParam("idioma", $idioma);
		$stmt->execute();
		$galeria = $stmt->fetchAll(PDO::FETCH_OBJ);
		$db = null;
		echo $_GET['callback']."(".json_encode($galeria).");";
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

?>